/*
 * Copyright 2019 Flipkart Internet Pvt. Ltd.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.flipkart.ohos.proteus.processor;

import ohos.agp.components.Component;
import ohos.app.Context;
import ohos.global.resource.NotExistException;
import ohos.global.resource.WrongTypeException;
import ohos.global.resource.solidxml.Theme;

import com.flipkart.ohos.proteus.ProteusContext;
import com.flipkart.ohos.proteus.ProteusView;
import com.flipkart.ohos.proteus.value.AttributeResource;
import com.flipkart.ohos.proteus.value.Color;
import com.flipkart.ohos.proteus.value.Resource;
import com.flipkart.ohos.proteus.value.StyleResource;
import com.flipkart.ohos.proteus.value.Value;

import java.io.IOException;

public abstract class ColorResourceProcessor<V extends Component> extends AttributeProcessor<V> {
    public static Color.Result evaluate(Value value, ProteusView view) {
        final Color.Result[] result = new Color.Result[1];
        ColorResourceProcessor<Component> processor = new ColorResourceProcessor<Component>() {
            @Override
            public void setColor(Component view, int color) {
                result[0] = Color.Result.color(color);
            }

            @Override
            public void setColor(Component view, ohos.agp.utils.Color color) {
                result[0] = Color.Result.color(color);
            }
        };
        processor.process(view.getAsView(), value);
        return result[0];
    }

    public static Value staticCompile(Value value, Context context) {
        if (null == value) {
            return Color.Int.BLACK;
        }
        if (value.isColor()) {
            return value;
        } else if (value.isObject()) {
            return Color.valueOf(value.getAsObject(), context);
        } else if (value.isPrimitive()) {
            Value precompiled = AttributeProcessor.staticPreCompile(value.getAsPrimitive(), context, null);
            if (null != precompiled) {
                return precompiled;
            }
            return Color.valueOf(value.getAsString(), Color.Int.BLACK);
        } else {
            return Color.Int.BLACK;
        }
    }

    @Override
    public void handleValue(final V view, Value value) {
        if (value.isColor()) {
            apply(view, value.getAsColor());
        } else {
            process(view, precompile(value, view.getContext(),
                ((ProteusContext) view.getContext()).getFunctionManager()));
        }
    }

    @Override
    public void handleResource(V view, Resource resource) {
        Theme colors = resource.getElement(view.getContext());
        if (null != colors) {
            int color = 0;
            try {
                color = colors.getColorValue(view.getName());
            } catch (IOException | NotExistException | WrongTypeException e) {
                e.printStackTrace();
            }
            setColor(view, color);
        } else {
            Integer color = resource.getColor(view.getContext());
            setColor(view, null == color ? Color.Int.BLACK.value : color);
        }
    }

    @Override
    public void handleAttributeResource(V view, AttributeResource attribute) {
        ohos.global.resource.Element element = attribute.apply(view.getContext());
        int color = 0;
        try {
            color = element.getColor();
        } catch (IOException | NotExistException | WrongTypeException e) {
            e.printStackTrace();
        }
        set(view, color);
    }

    @Override
    public void handleStyleResource(V view, StyleResource style) {
        ohos.global.resource.Element element = style.apply(view.getContext());
        int color = 0;
        try {
            color = element.getColor();
        } catch (IOException | NotExistException | WrongTypeException e) {
            e.printStackTrace();
        }
        set(view, color);
    }

    private void set(V view, int color) {
        if (color != 0) {
            setColor(view, color);
        } else {
            setColor(view, ohos.agp.utils.Color.BLACK);
        }
    }

    private void apply(V view, Color color) {
        Color.Result result = color.apply(view);
        if (result.colors != 0) {
            setColor(view, result.colors);
        } else {
            setColor(view, result.color);
        }
    }

    public abstract void setColor(V view, int color);

    public abstract void setColor(V view, ohos.agp.utils.Color color);

    @Override
    public Value compile(Value value, Context context) {
        return staticCompile(value, context);
    }
}
